import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/views/home/home.vue'
import Cara from '@/views/home/cara.vue'
import Carb from '@/views/home/carb.vue'
import Card from '@/views/home/card.vue'
import Register from '@/views/login/register.vue'
import Login from '@/views/login/login.vue'
import Carc from '@/views/home/carc.vue'
import Ss from '@/views/ss.vue'
import Dd from '@/views/dd.vue'
import Tj from '@/views/tj.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/home',
      name: 'home',
      component: HomeView,
      children:[
        {path: 'cara',name: 'cara',component: Cara,},
        {path: 'carb',name: 'carb',component: Carb,},
        {path: 'carc',name: 'carc',component: Carc,},
        {path: 'card',name: 'card',component: Card,},
      ]
    },
    {path: '/register',name: 'register',component: Register,},
    {path: '/login',name: 'login',component: Login,},
    {path: '/ss',name: 'ss',component: Ss,},
    {path: '/dd/:goods_id',name: 'dd',component: Dd,},
    {path: '/tj',name: 'tj',component: Tj,},
  ],
})
router.beforeEach((to, from, next) => {
  let token=localStorage.getItem('token')
  if(!token){
    if(to.fullPath=='/home/carc'||to.fullPath=='/home/card'){
      next('/login')
    }else{
      next()
    }
  }else{
    next()
  }
  
})
export default router
